package com.soonfor.evaluate.http.api;

import android.content.Context;

import com.estronger.shopmanage.utils.PrefUtils;
import com.google.gson.Gson;
import com.soonfor.evaluate.bean.EvalCustSaveBean;
import com.soonfor.evaluate.http.httptools.AsyncUtils;
import com.soonfor.evaluate.http.httptools.AsyncUtils.AsyncCallback;
import com.soonfor.evaluate.tools.UserInfo;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

public class Request {

    public static final int OPEN_FILE_BY_URL = 11;//打开网络文件

    /**
     * 其它任务
     */
    public static class OtherTask {
        public static final int GET_OTHERTASKNUM = 1522;
        public static final int GET_OTHERTASKLIST = 1523;
        public static final int GET_OTHERTASK_DETAIL = 1524;
        public static final int GET_OTHERTASK_RESULT = 1525;
        public static final int UPDATE_TASK_RESULT = 1526;
        public static final int GET_RETURNVISIT_PRJ = 1527;//回访题目

        //获取其它任务各状态的任务数量
        public static void getOtherTaskNum(Context context, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("sf_action", "getplanworklistnum");
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, GET_OTHERTASKNUM, true,callback);
        }

        /**
         * 获取其它任务列表
         *
         * @param context
         * @param taskStatus 任务状态 1.进行中，2.已逾期，3.已完成
         * @param pageIndex  页码
         * @param callback
         */
        public static void getOtherTaskList(Context context, int taskStatus, int pageIndex, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("sf_action", "getplanworklist");
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            params.put("getdatatype", taskStatus + "");
            params.put("pageindex", pageIndex + "");
            params.put("pagesize", "10");
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, GET_OTHERTASKLIST, true, callback);
        }

        //获取任务详情
        public static void getOtherTaskDetailInfo(Context context, String taskid, int requestCode, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("sf_action", "getplanworkdetail");
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            params.put("fplantaskid", taskid);
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, requestCode, true,callback);
        }

        //获取回访题目
        public static void getReturnVisitPrj(Context context, String taskid, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("sf_action", "getrtnvisitprj");
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            params.put("fplantaskid", taskid);
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, GET_RETURNVISIT_PRJ, true,callback);
        }

        //更新任务结果
        public static void updateTaskResult(Context context, Map<String, String> params, AsyncCallback callback) {
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, UPDATE_TASK_RESULT, true,callback);
        }
    }

    public static class Grab {
        public static final int GET_GRABORDER_TASK_LIST = 101;
        public static final int POST_GRAB = 102;

        /**
         * 获取待抢单任务列表
         */
        public static void getGraborderList(Context context, int pageNo, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("sf_action", "getsnatchpoollist");
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            params.put("pageindex", pageNo + "");
            params.put("pagesize", "10");
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, GET_GRABORDER_TASK_LIST, true,callback);
        }

        /**
         * 抢单操作
         */
        public static void postToGrab(Context context, String taskid, int taskType, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("sf_action", "snatchtask");
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            params.put("ftaskid", taskid);
            params.put("ftasktype", taskType + "");
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, POST_GRAB, true, callback);

        }
    }

    /**
     * 评价相关接口及口令
     */
    public static class Evaluate {
        public static final int GET_EVALUATEVIEW = 1527;
        public static final int GET_EVALUATETOMELIST_Y = 1528;
        public static final int GET_EVALUATETOMELIST_N = 1529;
        public static final int GET_EVALUATE_ITOCUSTOMER_Y = 1530;
        public static final int GET_EVALUATE_ITOCUSTOMER_N = 1531;
        public static final int GET_EVALUATETOMEDETAILINFO = 1532;
        public static final int GET_EVALUATE_ITOCUSTOMERINFO = 1533;
        public static final int POST_GETEVALUATETEMP = 1534;
        public static final int POST_SAVEEVALCUSTOMER = 1535;
        public static final int GET_EVALUATERANKING = 1536;
        public static final int GET_QUESTIONNAIRE_RESULT = 1537;

        /**
         * 获取评价概览
         *
         * @param context
         * @param callback
         */
        public static void getEvaluateView(Context context, AsyncCallback callback) {
            //获取评价排行榜数据
            getEvaluateRanking(context, GET_EVALUATEVIEW, callback);
        }


        //获取评价排行榜
        public static void getEvaluateRanking(Context cxt, int requestCode, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("sf_action", "getappraiserank");
            params.put("usercode", PrefUtils.getString(cxt, "userName", ""));
            AsyncUtils.post(cxt, UserInfo.COMMON_URL_BASE, params, requestCode, true,callback);
        }

        /**
         * 获取评价列表
         *
         * @param context
         * @param datatype       评价类型  0.客户评价，1.评价客户
         * @param taskatate      评价任务状态 0.待评价，1.已评价
         * @param pageIndex//当前页
         * @param callback
         */
        public static void getEvaluateList(Context context, int datatype, int taskatate, int pageIndex, int requestCode, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("sf_action", "getappraiseresultlist");
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            params.put("getdatatype", datatype + "");
            params.put("getdatastate", taskatate + "");
            params.put("pageindex", pageIndex + "");
            params.put("pagesize", 10 + "");//每一页的数目
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, requestCode, true,callback);
        }

        /**
         * 获取评价详情
         *
         * @param context
         * @param datatype    评价类型 0.客户评价，1.评价客户
         * @param sousetype   评价来源类型 0.任务，1.量尺，2.设计，3.复尺，4.放样，5.送货，6.安装
         * @param souseId     评价来源id
         * @param requestCode
         * @param callback
         */
        public static void getEvaluateDetailInfo(Context context, int datatype, int sousetype, String souseId, int requestCode, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            params.put("sf_action", "getappraisedetail");
            params.put("getdatatype", datatype + "");
            params.put("fappraiseoritype", sousetype + "");
            params.put("fappraiseoriid", souseId);
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, requestCode, true, callback);
        }

        /**
         * 获取评价项目
         *
         * @param context
         * @param datatype    评价类型 0.客户评价，1.评价客户
         * @param sousetype   评价来源类型 0.任务，1.量尺，2.设计，3.复尺，4.放样，5.送货，6.安装
         * @param souseId     评价来源id
         * @param requestCode
         * @param callback
         */
        public static void getAppraiseprj(Context context, String datatype, String sousetype, String souseId, int requestCode, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("sf_action", "getappraiseprj");
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            params.put("getdatatype", datatype + "");
            params.put("fappraiseoritype", sousetype);
            params.put("fappraiseoriid", souseId);
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, requestCode, true,callback);
        }

        /**
         * 评价客户（保存）
         *
         * @param context  datatype 评价类型 0.客户评价，1.评价客户
         *                 sousetype 评价来源类型 0.任务，1.量尺，2.设计，3.复尺，4.放样，5.送货，6.安装
         *                 souseId 评价来源id
         *                 falleval 整体评价结果 1.好评，2.中评，3.好评
         *                 evalContent 评价内容
         *                 starJson 星级评价对象 [{“fappraiseitemid”:0,//评价分类id“fpoint”,0//得分}]
         *                 imgsjson 评价图片对象 [{ “fpath”:”E:\\img\img.png”,//路径 “ffilename”:”img.png”,//图片名 }]
         * @param callback
         */
        public static void saveEvaluateCustomer(Context context, EvalCustSaveBean saveBean, AsyncCallback callback) {
            Gson gson = new Gson();
            com.loopj.android.http.RequestParams params = new com.loopj.android.http.RequestParams();
            params.put("sf_action", "saveappraise");
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            params.put("getdatatype", saveBean.getGetdatatype());
            params.put("fappraiseoritype", saveBean.getFappraiseoritype());
            params.put("fappraiseoriid", saveBean.getFappraiseoriid());
            params.put("fallhighappraiseresult", saveBean.getFallhighappraiseresult());
            params.put("fappraisecontent", saveBean.getFappraisecontent());
            params.put("appraisestars", saveBean.getJsonStrFromReSortItemDtos());
            params.put("appraiseimgs", gson.toJson(saveBean.getAppraiseimgs()));
            try {
                AsyncUtils.postToSubmit(context, UserInfo.COMMON_URL_BASE, params, POST_SAVEEVALCUSTOMER, false, callback);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //获取回访结果
        public static void getReturnVisitResult(Context context, String taskid, AsyncCallback callback) {
            Map<String, String> params = new HashMap<>();
            params.put("sf_action", "getrtnvisitresult");
            params.put("usercode", PrefUtils.getString(context, "userName", ""));
            params.put("fplantaskid", taskid);
            AsyncUtils.post(context, UserInfo.COMMON_URL_BASE, params, GET_QUESTIONNAIRE_RESULT, true,callback);
        }

        /**
         * 回复说明
         */
        public static void replyToThat(Context cxt, int requestcode, String  url, Map<String, String> params, AsyncCallback callback) {
            AsyncUtils.post(cxt, url, params, requestcode, false, callback);
        }

    }

    /**
     * @param file
     * @param callback
     * @param requestcode
     */
    public static void postFile(Context context, File file, AsyncCallback callback, int requestcode) {
        com.loopj.android.http.RequestParams params = new com.loopj.android.http.RequestParams();
        try {
            params.put("imgFile", file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //params.addBodyParameter("dir", "image");
        AsyncUtils.postToSubmit(context, UserInfo.UPLOAD_FILE_BASE, params, requestcode, false, callback);
    }

    public static void openFileByUrl(Context context, String imagPath, AsyncUtils.AsyncCallback callback) {
        AsyncUtils.get(context, imagPath, OPEN_FILE_BY_URL, callback);
    }
}
